<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>请假单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!--搜索开始-->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="searchFrm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">请假标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">请假内容</label>
                            <div class="layui-input-inline">
                                <input type="text" name="content" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">开始时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="startTime" id="startTime" readonly="readonly"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">结束时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="endTime" id="endTime" readonly="readonly" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-block" style="text-align: center;">
                                <a class="layui-btn layui-btn-primary" lay-submit="" lay-filter="data-search-btn"><label
                                        class="layui-icon layui-icon-search"></label>搜索</a>
                                <a class="layui-btn layui-btn-primary" onclick="javascript:$('#searchFrm')[0].reset()"
                                ><label class="layui-icon layui-icon-refresh"></label>重置</a>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <!--搜索结束-->

        <!--添加和修改的弹出层开始-->
        <div id="addOrUpdateDiv" style="display: none;padding: 0.3125rem">
            <form method="post" class="layui-form layui-form-pane" lay-filter="dataFrm" id="dataFrm">
                <div class="layui-form-item">
                    <label class="layui-form-label">请假标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" id="username" lay-verify="required" autocomplete="off"
                               placeholder="请输入请假单标题" class="layui-input">
                        <input type="hidden" name="state">
                        <input type="hidden" name="userid">
                        <input type="hidden" name="id">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">请假天数</label>
                        <div class="layui-input-inline">
                            <input type="tel" name="days" lay-verify="required|number" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">请假时间</label>
                        <div class="layui-input-inline">
                            <input type="text" name="leavetime" readonly="readonly" autocomplete="off"
                                   class="layui-input" id="leavetime" placeholder="yyyy-MM-dd HH:mm:ss">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">请假原因</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入请假单内容" name="content" id="content" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item" style="text-align: center;">
                    <button class="layui-btn" lay-submit="" lay-filter="addLeaveBill">提交</button>
                    <button type="reset" class="layui-btn layui-btn-warm">重置</button>
                </div>
            </form>
        </div>

        <div id="viewProcess" style="display: none;padding: 0.3125rem">
            <form class="layui-form" method="post" id="frm">
                <div class="layui-form-item">
                    <label class="layui-form-label">请假标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" id="title" disabled="disabled" class="layui-input">

                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">请假天数</label>
                        <div class="layui-input-inline">
                            <input type="tel" name="days" id="day" disabled="disabled" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">请假时间</label>
                        <div class="layui-input-inline">

                            <input type="text" name="leavetime" id="leavetime_two" disabled="disabled"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">请假原因</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入请假单内容" disabled="disabled" name="content" id="content_two"
                                  class="layui-textarea">${leaveBill.content }</textarea>
                    </div>
                </div>
            </form>

            <table id="commentList" lay-filter="commentList"></table>
        </div>
        <!--添加和修改的弹出层结束-->


        <!--表格开始-->
        <table id="leaveBillList" lay-filter="leaveBillList"></table>
        <!--表格结束-->
    </div>
</div>


<script type="text/html" id="leaveBillToolBar">
    <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add">添加</a>
</script>

<!--操作-->
<script type="text/html" id="leaveBillListBar">
    {{#  if(d.state==0){ }}
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="startProcess">提交申请</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    {{#  } else if(d.state==1){ }}
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="viewSpProcess">审批进度查询</a>
    {{#  } else if(d.state==2){ }}
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="viewSpProcess">审批进度查询</a>
    {{#  } else if(d.state==3){ }}
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="viewSpProcess">审批进度查询</a>
    {{#  } }}
</script>

<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../../lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="../../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../../lib/common/common.js" charset="utf-8"></script>

<script>
    let tableIns;
    let index;
    layui.use(['form', 'layer', 'table', 'laydate', 'laytpl'], function () {
        var form = layui.form,
            layer = layui.layer;
        $ = layui.jquery,
            laydate = layui.laydate,
            laytpl = layui.laytpl,
            table = layui.table;

        laydate.render({
            elem: "#startTime"
        });
        laydate.render({
            elem: "#endTime"
        });

        laydate.render({
            elem: "#leavetime",
            type: 'datetime'
        });

        //请假单列表
        tableIns = table.render({
            elem: '#leaveBillList',
            url: api + '/leaveBill/loadAllLeaveBills',
            cellMinWidth: true,
            toolbar: '#leaveBillToolBar',
            page: true,
            id: "leaveBillListTable",
            cols: [[
                {type: "checkbox"},
                {field: 'id', title: '请假单ID', align: "center"},
                {field: 'title', title: '请假单标题', align: "center"},
                {field: 'content', title: '请假内容', align: "center"},
                {field: 'days', title: '请假天数', align: "center"},
                {field: 'leavetime', title: '请假时间', align: "center"},
                {
                    field: 'state', title: '状态', align: "center", templet: function (d) {
                        var html = "";
                        if (d.state == 0) {
                            html = '<span class="layui-badge layui-bg-red">未提交</span>';
                        } else if (d.state == 1) {
                            html = '<span class="layui-badge layui-bg-orange">审批中</span>';
                        } else if (d.state == 2) {
                            html = '<span class="layui-badge layui-bg-blue">审批完成</span>';
                        } else if (d.state == 3) {
                            html = '<span class="layui-badge layui-bg-gray">已放弃</span>';
                        } else {
                            html = "<font color=red>未知状态</font>"
                        }
                        return html;
                    }
                },
                {title: '操作', templet: '#leaveBillListBar', align: "center"}
            ]],
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var params = $("#searchFrm").serialize();
            //执行搜索重载
            table.reload('leaveBillListTable', {
                page: {
                    curr: 1
                }
                , url: api + 'leaveBill/loadAllLeaveBills?' + params
            });
            return false;
        });

        //监听表头的事件
        table.on('toolbar(leaveBillList)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    openAddLeaveBill();
                    break;
            }
        });

        //监听行数据的事件
        table.on('tool(leaveBillList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除这条数据么', function (index) {
                    $.post(api + "leaveBill/deleteLeaveBill", {id: data.id}, function (res) {
                        layer.msg(res.msg);
                        obj.del();
                        layer.close(index);
                    })
                });
            } else if (obj.event === 'edit') {
                openUpdateLeaveBill(data);
            } else if (obj.event === 'startProcess') {
                startProcess(data);//请假单的对象
            } else if (obj.event === 'viewSpProcess') {
                viewSpProcess(data);
            }
        });

        //查看审批进度
        function viewSpProcess(data) {
            index = layui.layer.open({
                title: data.title + "的审批进度",
                type: 1,
                area: ["800px", "500px"],
                content: $("#viewProcess"),
                success: function (layero, index) {

                    $('#title').val('');
                    $('#day').val('');
                    $('#leavetime_two').val('');
                    $('#content_two').val('');

                    //装载数据表格
                    $('#title').val(data.title);
                    $('#day').val(data.days);
                    $('#leavetime_two').val(data.leavetime);
                    $('#content_two').val(data.content);

                    table.render({
                        elem: '#commentList',
                        url: api + 'workFlow/loadCommentByLeaveBillId?id='+data.id,
                        cellMinWidth: true,
                        id: "commentListTable",
                        cols: [[
                            {field: 'time', title: '批注时间', align: "center"},
                            {field: 'userId', title: '批注人', align: "center"},
                            {field: 'message', title: '批注内容', align: "center"}
                        ]]
                    });

                    setTimeout(function () {
                        layui.layer.tips('点击此处返回请假单列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500)
                }
            })
        }

        //启动流程
        function startProcess(data) {
            layer.confirm('确定要提交【' + data.title + '】请假单吗？', {icon: 3, title: '提示信息'}, function (index) {
                $.post(api + "workFlow/startProcess", {
                    id: data.id  //将需要删除的id作为参数传入
                }, function (data) {
                    layer.msg(data.msg);
                    //刷新table
                    tableIns.reload();
                    //关闭提示框
                    layer.close(index);
                })
            });
        }

        //打开添加请假的弹层
        var mainIndex;
        var url;  //动态的url
        function openAddLeaveBill() {
            mainIndex = layer.open({
                type: 1,
                title: '添加请假',
                content: $("#addOrUpdateDiv"),
                area: ['800px', '600px'],
                success: function (index) {
                    url = api + "leaveBill/addLeaveBill";
                    //重置表单
                    $("#dataFrm")[0].reset();

                    $("#username").val("【" + localStorage.getItem("username") + "】的请假单");

                    setTimeout(function () {
                        layui.layer.tips('点击此处返回请假单列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500);
                }
            });
        }

        function openUpdateLeaveBill(data) {
            mainIndex = layer.open({
                type: 1,
                title: '修改[' + data.title + ']请假',
                content: $("#addOrUpdateDiv"),
                area: ['800px', '600px'],
                success: function (index) {
                    url = api + "leaveBill/updateLeaveBill";
                    //装载数据表格
                    form.val("dataFrm", data);

                    setTimeout(function () {
                        layui.layer.tips('点击此处返回请假单列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500);
                }
            });
        }

        //提交数据
        form.on("submit(addLeaveBill)", function (data) {
            var params = $("#dataFrm").serialize();
            $.post(url, params, function (res) {
                layer.msg(res.msg);
                if (res.code == 200) {
                    tableIns.reload();
                }
                layer.close(mainIndex);
            });
            return false;
        });
    });
</script>

</body>
</html>
